//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 9
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using ProxyHelpers.EWS;

namespace CreateRecurringSeriesWithChangingTimeZone
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            CreateRecurringSeriesWithChangingTimeZone(binding);
        }

        /// <summary>
        /// Create a daily recurring series with a time zone that adheres to daylight 
        /// savings time via the proxy (Listing 9-56)
        /// </summary>
        /// <param name="binding">Binding to use for the web service call</param>
        /// <returns>Id of newly created recurring master for the series</returns>
        /// 
        public static ItemIdType CreateRecurringSeriesWithChangingTimeZone(
            ExchangeServiceBinding binding)
        {
            DateTime nov2nd20078amUnspec = new DateTime(
                2007, 11, 02, 08, 00, 00, DateTimeKind.Unspecified);
            DateTime twoAM = new DateTime(
                DateTime.Now.Year, DateTime.Now.Month,
                DateTime.Now.Day, 02, 00, 00, DateTimeKind.Unspecified);

            // Basic new calendar item information
            //
            CalendarItemType newRecurringMaster = new CalendarItemType();
            newRecurringMaster.Start = nov2nd20078amUnspec;
            newRecurringMaster.StartSpecified = true;
            newRecurringMaster.End = nov2nd20078amUnspec.AddHours(1);
            newRecurringMaster.EndSpecified = true;
            newRecurringMaster.Subject = "Four Day Recurring Series in" +
                "US Mountain Standard Time";

            // Recurrence information for the new series
            // 
            DailyRecurrencePatternType recurrencePattern =
                new DailyRecurrencePatternType();
            recurrencePattern.Interval = 1;
            NumberedRecurrenceRangeType recurrenceRange =
                new NumberedRecurrenceRangeType();
            recurrenceRange.StartDate = nov2nd20078amUnspec;
            recurrenceRange.NumberOfOccurrences = 4;

            // We want to create this new series in the "Mountain Time
            // Zone" which is defined as UTC -07:00 and adheres to a 
            // Daylight Savings Time.  DLT starts on the second Sunday in 
            // March at 2am and ends on the first Sunday in November at 2am.
            // The Daylight Time bias is -06:00
            //
            // We will use two RelativeYearlyRecurrencePatterns to
            // describe the start of Standard and Daylight time
            // accordingly
            //
            RelativeYearlyRecurrencePatternType standardTimeDesc =
                new RelativeYearlyRecurrencePatternType();
            standardTimeDesc.DaysOfWeek = DayOfWeekType.Sunday.ToString();
            standardTimeDesc.DayOfWeekIndex = DayOfWeekIndexType.First;
            standardTimeDesc.Month = MonthNamesType.November;

            RelativeYearlyRecurrencePatternType daylightTimeDesc =
                new RelativeYearlyRecurrencePatternType();
            daylightTimeDesc.DaysOfWeek = DayOfWeekType.Sunday.ToString();
            daylightTimeDesc.DayOfWeekIndex = DayOfWeekIndexType.Second;
            daylightTimeDesc.Month = MonthNamesType.March;

            // We've defined on what date the time change occurs, but we
            // still have to define how far from the base offset the time
            // change is, and at what time of day the time change occurs
            //
            TimeChangeType standardTimeChangeInfo = new TimeChangeType();
            standardTimeChangeInfo.Item = standardTimeDesc;
            standardTimeChangeInfo.Offset = "PT0H";
            standardTimeChangeInfo.Time = twoAM;

            TimeChangeType daylightTimeChangeInfo = new TimeChangeType();
            daylightTimeChangeInfo.Item = daylightTimeDesc;
            daylightTimeChangeInfo.Offset = "-PT1H";
            daylightTimeChangeInfo.Time = twoAM;

            // Create the meeting time zone object and assign our Daylight
            // and Standard time change definitions accordingly
            //
            TimeZoneType meetingTimeZone = new TimeZoneType();
            meetingTimeZone.BaseOffset = "PT7H";
            meetingTimeZone.Standard = standardTimeChangeInfo;
            meetingTimeZone.Daylight = daylightTimeChangeInfo;

            // Assign the Time Zone to our new series
            //
            newRecurringMaster.MeetingTimeZone = meetingTimeZone;

            // Set the Recurring information on our new series
            //
            newRecurringMaster.Recurrence = new RecurrenceType();
            newRecurringMaster.Recurrence.Item = recurrencePattern;
            newRecurringMaster.Recurrence.Item1 = recurrenceRange;

            // Make our request
            //
            CreateItemType createItemRequest = new CreateItemType();
            createItemRequest.SendMeetingInvitations =
                CalendarItemCreateOrDeleteOperationType.SendToNone;
            createItemRequest.SendMeetingInvitationsSpecified = true;
            createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
            createItemRequest.Items.Items = new CalendarItemType[1];
            createItemRequest.Items.Items[0] = newRecurringMaster;

            CreateItemResponseType response = binding.CreateItem(createItemRequest);
            ItemInfoResponseMessageType responseMessage =
                response.ResponseMessages.Items[0] as ItemInfoResponseMessageType;

            if (responseMessage.ResponseCode != ResponseCodeType.NoError)
            {
                throw new Exception("CreateCalendarItem failed with response code " +
                         responseMessage.ResponseCode.ToString());
            }
            return responseMessage.Items.Items[0].ItemId;
        }

    }
}
